package com.example.fw;

import org.openqa.selenium.By;
import com.example.tests.ContactDate;
import com.example.utils.SortedListOf;

import java.util.List;

import org.openqa.selenium.WebElement;

public class ContactHelper extends HelperBase {

	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public ContactHelper createContact(ContactDate contact, boolean CREATION) {
		manager.navigateTo().addNewContact();
		fillContactForm(contact, CREATION);
		submitContactCreation();
		returnToContactPage();
		rebuildCache();
		return this;
	}

	public ContactHelper modifyContact(int index, ContactDate contact,
			boolean CREATION) {
		manager.navigateTo().mainPage();
		initEditContact(index);
		fillContactForm(contact, CREATION);
		modificationContact();
		returnToContactPage();
		rebuildCache();
		return this;
	}

	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		initEditContact(index);
		clickDeleteContact();
		returnToContactPage();
		rebuildCache();
		return this;
	}

	private SortedListOf<ContactDate> cachedContacts;

	public SortedListOf<ContactDate> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;

	}

	private void rebuildCache() {
		manager.navigateTo().mainPage();
		cachedContacts = new SortedListOf<ContactDate>();
		List<WebElement> trs = driver
				.findElements(By.xpath("//tbody/tr/td[2]"));
		for (WebElement tr : trs) {

			String last = tr.getText();
			if (last.equals("Select all")) {
			} else {
				cachedContacts.add(new ContactDate().withLast(last));
			}
		}
	}

	// -----------------------------------------------------------

	public ContactHelper returnToContactPage() {
		click(By.linkText("home page"));
		return this;
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		return this;
	}

	public ContactHelper fillContactForm(ContactDate contact, boolean formType) {
		type(By.name("firstname"), contact.getFirst());
		type(By.name("lastname"), contact.getLast());
		type(By.name("address"), contact.getAddress());
		type(By.name("home"), contact.getTelephoneHome());
		type(By.name("mobile"), contact.getTelephoneMobile());
		type(By.name("work"), contact.getTelephoneWork());
		type(By.name("email"), contact.getEmail());
		type(By.name("email2"), contact.getEmail2());
		if (formType == CREATION) {
			// selectByText(By.name("new_group"), "group 1");
		} else {
			if (driver.findElements(By.name("new_group")).size() != 0) {
				throw new Error(
						"Group selector exist in contact modification form");
			}
		}
		selectByText(By.name("bday"), contact.getBirthDay());
		selectByText(By.name("bmonth"), contact.getBirthMonth());

		type(By.name("byear"), contact.getBirthYear());
		type(By.name("address2"), contact.getAddress2());
		type(By.name("phone2"), contact.getTelephoneHome2());
		return this;

	}

	public ContactHelper initEditContact(int index) {

		click(By.xpath("//tr[" + (index + 2) + "]/td[7]"));
		return this;
	}

	public ContactHelper clickDeleteContact() {
		click(By.xpath("//input[2][@name='update']"));
		return this;
	}

	public ContactHelper modificationContact() {
		click(By.name("update"));
		return this;
	}

}
